<?php
//Create by Nguyen 15/08/2012
require_once 'openid.php';
require_once('../../../config.php');
require_once(DIR_SYSTEM . 'database/mysql.php');
require_once (DIR_SYSTEM .'helper/customer.php');
require_once (DIR_SYSTEM.'engine/registry.php');
require_once (DIR_SYSTEM.'library/db.php');
require_once (DIR_SYSTEM.'library/session.php');



try {
    # Change 'localhost' to your domain name.
    $openid = new LightOpenID('http://99xu.vn');
    if(!$openid->mode) {
        if(isset($_GET['openid_identifier'])) {
            $openid->identity = $_GET['openid_identifier'];
			$openid->required = array('namePerson/friendly', 'contact/email','namePerson','namePerson/first','namePerson/last','phone/fax','phone/cell','contact/IM/Skype','contact/IM/Yahoo');
            header('Location: ' . $openid->authUrl());
        }
    } elseif($openid->mode == 'cancel') {
        //echo 'User has canceled authentication!';
		header('Location:' .HTTP_SERVER. 'index.php');
    } else {
		$returnVariables = $openid->getAttributes();
		$email = $returnVariables['contact/email'];
		$firstname = $returnVariables['namePerson/first'];
		$lastname = $returnVariables['namePerson/last'];
		$cellphone = $returnVariables['phone/cell'];
		$fax = $returnVariables['phone/fax'];
		$yahoo = $returnVariables['contact/IM/Yahoo'];
		$skype = $returnVariables['contact/IM/Skype'];
        $mysql = new mysql(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
		$customer_query = $mysql->query("SELECT * FROM " . DB_PREFIX . "customer where LOWER(email) = '" . $mysql->escape(strtolower($email)) . "' AND status = '1'");
		if ($customer_query->num_rows)
			{
				$session = new Session();
				Registry::set('session', $session);

				$db = new DB(DB_DRIVER, DB_HOSTNAME, DB_USERNAME, DB_PASSWORD, DB_DATABASE);
				Registry::set('db', $db);

				$login=new HelperCustomer();
				$login->login($email,'',true);
				//$login->session->data['customer_id'] = $customer_query->row['customer_id'];
				//$success = true;
				header('Location:'.HTTP_SERVER.'index.php?route=account/account');
				
			}
		else header('Location:' .HTTP_SERVER. 'index.php?route=account/create');
    }
} catch(ErrorException $e) {
    echo $e->getMessage();
}
?>
